package net.media.datafw.model;

import java.lang.reflect.Method;
import java.lang.reflect.Type;

import net.media.datafw.DataException;

public class Field {
	protected String fieldName;
	protected Method getMethod;
	protected Method setMethod;
	protected Type type;

	public Field(Method getMethod, Method setMethod) {
		this.getMethod = getMethod;
		this.setMethod = setMethod;
		this.fieldName = getMethod.getName().substring("get".length());
		this.fieldName = String.valueOf(this.fieldName.charAt(0)).toLowerCase() + this.fieldName.substring(1);
		this.type = getMethod.getGenericReturnType();
	}

	public String getName() {
		return this.fieldName;
	}

	public Type getType() {
		return this.type;
	}

	public void setValue(Object object, Object value) throws DataException {
		try {
			this.setMethod.invoke(object, value);
		} catch (Exception e) {
			throw new DataException(e);
		}
	}

	public Object getValue(Object object) throws DataException {
		try {
			return this.getMethod.invoke(object);
		} catch (Exception e) {
			throw new DataException(e);
		}
	}

}
